package com.ae.shield.generator.builder.recipes.assembly;

import com.google.gson.JsonObject;
import com.ae.shield.common.enchantment.EnchantmentWrap;
import com.ae.shield.common.items.ItemList;
import com.ae.shield.common.recipe.ModRecipesType;
import net.minecraft.advancements.Advancement;
import net.minecraft.data.IFinishedRecipe;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.Item;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.tags.ITag;
import net.minecraft.util.ResourceLocation;

import javax.annotation.Nullable;
import java.util.List;
import java.util.function.Consumer;

public class SuffixUpgradeAssemblyBuilder extends AssemblyRecipeBuilder<SuffixUpgradeAssemblyBuilder>{
    private  EnchantmentWrap enIn;
    private final EnchantmentWrap enOut;

    public SuffixUpgradeAssemblyBuilder(Enchantment suffix, int lvl) {
        super(ItemList.SUFFIX_KID.get(), 1);
        this.enOut = EnchantmentWrap.wrap(suffix, lvl);
        addBacking(ItemList.SUFFIX_KID.get());
    }

    public SuffixUpgradeAssemblyBuilder addOldSuffix(Enchantment suffix, int lvl) {
        enIn = EnchantmentWrap.wrap(suffix, lvl);
        return this;
    }

    public static SuffixUpgradeAssemblyBuilder recipe(Enchantment suffixOut, int lvlOut) {
        return new SuffixUpgradeAssemblyBuilder(suffixOut, lvlOut);
    }

    @Override
    public void build(Consumer<IFinishedRecipe> consumer, ResourceLocation id) {
        id = new ResourceLocation(id.getNamespace(), "suffix/upgrade/" + id.getPath());
        super.build(consumer, id);
    }

    @Override
    public IFinishedRecipe getResult(ResourceLocation id) {
        return new Result(id, enIn, enOut, result, count, group == null ? "" : group, backing, ingredients, fluid, fluidTag, fluidCount, energy, tick, advancementBuilder);
    }

    @Override
    public void validate(ResourceLocation id) {
        super.validate(id);
        if (enIn == null || enOut == null) {
            throw new IllegalStateException("未定义效果(effect) " + id);
        }
    }

    public static class Result extends AssemblyRecipeBuilder.Result {
        private final EnchantmentWrap enIn;
        private final EnchantmentWrap enOut;

        public Result(ResourceLocation id, EnchantmentWrap enIn, EnchantmentWrap enOut, Item result, int count, String group, Ingredient backing, List<Ingredient> ingredients, @Nullable Fluid fluid, @Nullable ITag<Fluid> fluidTag, int fluidCount, int energy, int tick, Advancement.Builder advancementBuilder) {
            super(id, result, count, group, backing, ingredients, fluid, fluidTag, fluidCount, energy, tick, advancementBuilder);
            this.enIn = enIn;
            this.enOut = enOut;
        }

        @Override
        public void serialize(JsonObject json) {
            JsonObject in = new JsonObject();
            JsonObject out = new JsonObject();
            JsonObject suffix = new JsonObject();
            in.addProperty("suffix_name", enIn.getEn().getRegistryName().toString());
            in.addProperty("lvl", enIn.getLvl());
            out.addProperty("suffix_name", enOut.getEn().getRegistryName().toString());
            out.addProperty("lvl", enOut.getLvl());
            suffix.add("suffix_in", in);
            suffix.add("suffix_out", out);
            json.add("suffix", suffix);
            super.serialize(json);
        }

        @Override
        public IRecipeSerializer<?> getSerializer() {
            return ModRecipesType.SUFFIX_UPGRADE_ASSEMBLY_SERIALIZER.get();
        }
    }
}
